﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using One2Team.EasyScrum.UI.Web.Models;

namespace One2Team.EasyScrum.UI.Web.Controllers
{
    public class UserStoryController : Controller
    {
        private easyscrumEntities db = new easyscrumEntities();

        //
        // GET: /UserStory/

        public ActionResult Index()
        {
            var user_story = db.user_story.Include(u => u.priority).Include(u => u.product_backlog).Include(u => u.sprint_backlog).Include(u => u.team).Include(u => u.user_story_status);
            return View(user_story.ToList());
        }

        //
        // GET: /UserStory/Details/5

        public ActionResult Details(int id = 0)
        {
            user_story user_story = db.user_story.Find(id);
            if (user_story == null)
            {
                return HttpNotFound();
            }
            return View(user_story);
        }

        //
        // GET: /UserStory/Create

        public ActionResult Create()
        {
            ViewBag.id_priority = new SelectList(db.priority, "id", "text");
            ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom");
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom");
            ViewBag.id_team = new SelectList(db.team, "id", "teamname");
            ViewBag.id_user_story_status = new SelectList(db.user_story_status, "id", "text");
            return View();
        }

        //
        // POST: /UserStory/Create

        [HttpPost]
        public ActionResult Create(user_story user_story)
        {
            if (ModelState.IsValid)
            {
                db.user_story.Add(user_story);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_priority = new SelectList(db.priority, "id", "text", user_story.id_priority);
            ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", user_story.id_product_backlog);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", user_story.id_sprint_backlog);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user_story.id_team);
            ViewBag.id_user_story_status = new SelectList(db.user_story_status, "id", "text", user_story.id_user_story_status);
            return View(user_story);
        }


        //
        // GET: /UserStory/Edit/5

        public ActionResult Edit(int id = 0)
        {
            user_story user_story = db.user_story.Find(id);
            if (user_story == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_priority = new SelectList(db.priority, "id", "text", user_story.id_priority);
            ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", user_story.id_product_backlog);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", user_story.id_sprint_backlog);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user_story.id_team);
            ViewBag.id_user_story_status = new SelectList(db.user_story_status, "id", "text", user_story.id_user_story_status);
            return View(user_story);
        }
        //
        // GET: /UserStory/Edit/5
        // Pour Dev
        public ActionResult EditScrumMaster(int id = 0)
        {
            user_story user_story = db.user_story.Find(id);
            if (user_story == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_priority = new SelectList(db.priority, "id", "text", user_story.id_priority);
            ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", user_story.id_product_backlog);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", user_story.id_sprint_backlog);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user_story.id_team);
            ViewBag.id_user_story_status = new SelectList(db.user_story_status, "id", "text", user_story.id_user_story_status);
            return View(user_story);
        }



        //
        // POST: /UserStory/Edit/5

        [HttpPost]
        public ActionResult Edit(user_story user_story)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user_story).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_priority = new SelectList(db.priority, "id", "text", user_story.id_priority);
            ViewBag.id_product_backlog = new SelectList(db.product_backlog, "id", "nom", user_story.id_product_backlog);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", user_story.id_sprint_backlog);
            ViewBag.id_team = new SelectList(db.team, "id", "teamname", user_story.id_team);
            ViewBag.id_user_story_status = new SelectList(db.user_story_status, "id", "text", user_story.id_user_story_status);
            return View(user_story);
        }

        //
        // GET: /UserStory/Delete/5

        public ActionResult Delete(int id = 0)
        {
            user_story user_story = db.user_story.Find(id);
            if (user_story == null)
            {
                return HttpNotFound();
            }
            return View(user_story);
        }

        //
        // POST: /UserStory/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            user_story user_story = db.user_story.Find(id);
            db.user_story.Remove(user_story);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}